#include "mainui.h"
#include "ui_mainui.h"
#include "authmanager.h"
#include "msgbox.h"

MainUI::MainUI(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MainUI)
{
    ui->setupUi(this);
    bdf = BllDataFlow::getInstance();
    connect(bdf, &BllDataFlow::endExperimentSignal, this, &MainUI::setEnableSlot);
    connect(bdf, &BllDataFlow::retranslateUiSignal, this, &MainUI::retranslateUiSlot);


    initWidget();
}

MainUI::~MainUI()
{
    delete ui;
}

void MainUI::on_btnData_clicked()
{
    bdf->changeMainPageIndex(MainPageIndex::DATA);
    bdf->setCurrentTime(TimeSetType::DATA_START);
}

void MainUI::on_btnExp_clicked()
{
    User user = {};
    user.name = currentUsername;
    int permission = SqlHelper::getInstance()->selectUserPermission(user);
    if (AuthManager::instance()->hasPermission(permission, Experiment))
    {
        bdf->changeMainPageIndex(MainPageIndex::EXPERIMENT);
        bdf->changeExperimentPageIndex(0);
    }
    else
    {
        MsgBox::information(this, tr("Tips"), tr("unauthorized"));
    }
}

void MainUI::on_btnConfig_clicked()
{
    bdf->changeMainPageIndex(MainPageIndex::CONFIG);
}

void MainUI::retranslateUiSlot()
{
    ui->retranslateUi(this);
}

void MainUI::setEnableSlot(bool state)
{
    ui->btnConfig->setEnabled(state);
}



void MainUI::initWidget()
{
    if (windowWidth >= 1280) {
        this->setProperty("cssSize", "large");
    }
//    QString btnStyleSheet = "QToolButton{background:none;}";
//    ui->btnData->setStyleSheet(btnStyleSheet);
//    ui->btnExp->setStyleSheet(btnStyleSheet);
//    ui->btnConfig->setStyleSheet(btnStyleSheet);

//    QString promtWidgetStyleSheet = "QWidget{background:none;border-style:none;border:0px;}";
//    ui->cTime->setStyleSheet(promtWidgetStyleSheet);
}
